package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@DubboService
public class UserInApifoImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    //保存UserIn到数据库
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }


    //更新
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }


    /**
     *
     * 根据用户id查询UserInfo
     */
    public UserInfo selectUserInfo(Long userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        return userInfo;
    }



    //批量查询UserInfo
    public Map<Long, UserInfo> batchQueryUserInfo(List<Long> ids, UserInfo userInfo) {
        //1.构建查询条件
        QueryWrapper<UserInfo> qw =  new QueryWrapper<>();
        qw.in("id", ids);
        //2.如果UserInfo不为空，添加条件
        if (userInfo != null) {
            if(!StringUtils.isEmpty(userInfo.getGender())){
                qw.eq("gender",userInfo.getGender());
            }
            if(userInfo.getAge() != null){
                qw.lt("age", userInfo.getAge());
            }

            if(!StringUtils.isEmpty(userInfo.getNickname())){
                qw.like("nickname", userInfo.getNickname());
            }
        }

        //3.调用UserInfoMapper批量查询UserInfo
        List<UserInfo> userInfosList = userInfoMapper.selectList(qw);
        //4.把list集合转换为map集合
        Map<Long, UserInfo> userInfosMap = CollUtil.fieldValueMap(userInfosList, "id");

        return userInfosMap;
    }


}
